package com.project.myBeans;

import com.project.entity.AttendExam;
import com.project.entity.Exam;
import com.project.service.AttendExamService;
import com.project.service.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.List;

/**
 * @Author: TateBrown
 * @date: 2018/7/3 15:51
 * @param:
 * @return:
 */
@Component
@EnableAutoConfiguration
@RestController
public class SchedulingTask {

    @Autowired
    ExamService examService;

    @Autowired
    AttendExamService attendExamService;
    /**
     * @Description:定时扫描所有考试任务修改考试state.
     * @param: []
     * @return: void
     * @author: TateBrown
     * @Date: 2018/7/3
     */

    /**
     * @author: DragonDoor
     * @Date: 2018/10/24 13:01
     * @Description: 考试定时器修改
     */
    @Scheduled(fixedRate = 30000)//每隔一分钟查一次
    public void CheckExam() {
        List<Exam> list = examService.GetAllExam();
        Timestamp curtime = new Timestamp(System.currentTimeMillis());
        for (Exam tmp : list) {
            Timestamp tmpfinishtime = tmp.getFinishtime();
            Timestamp tmpstarttime = tmp.getStarttime();
            if (curtime.before(tmpstarttime)&&tmp.getState()!=1) {
                tmp.setState(1);
                examService.modify(tmp);
            } else if ((curtime.equals(tmpstarttime) || curtime.after(tmpstarttime)) && (curtime.equals(tmpfinishtime) || curtime.before(tmpfinishtime))&&tmp.getState()!=2) {
                tmp.setState(2);
                examService.modify(tmp);
            } else if((curtime.equals(tmpfinishtime)||curtime.after(tmpfinishtime))&&tmp.getState()!=3) {
                tmp.setState(3);
                examService.modify(tmp);
//                attendExamService.submitAllByExamId(tmp.getId(),tmp.getFinishtime());
            }
        }
    }
}
